package com.onlineclass.dao;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.demo.util.JDBCUtils;
import com.onlineclass.bean.User;

/**
 * 用户DAO
 * 
 * @author david
 *
 */
public class UserDAO {
	static QueryRunner qr = new QueryRunner();
	
	/**
	 * 用户注册
	 * @param user
	 * @return
	 */
	public long reg(User user) {
		long i = 0;
		try {
			i = qr.update(JDBCUtils.getConnection(), 
					"insert into t_user(mobile,password,regTime) values(?,?,now())",
					user.getMobile(), user.getPassword());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}

	/**
	 * 根据课程id查询相关课程：查同一个标签下的课程，查5个
	 * 
	 * @param id
	 * @return
	 */
	public long login(User user) {
		long i = 0;
		try {
			i = qr.query(JDBCUtils.getConnection(), "select count(1) nums from t_user where mobile=? and password=?",
					new ScalarHandler<Long>(), user.getMobile(), user.getPassword());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	/**
	 * 根据手机号去查该用户的信息
	 * @param mobile
	 * @return
	 */
	public User findByMobile(String mobile) {
		User user = null;
		try {
			user = qr.query(JDBCUtils.getConnection(), "select * from t_user where mobile=?",
					new BeanHandler<User>(User.class), mobile);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

}